package net.gamebookengine.viewer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.gamebookengine.viewer.helper.ToastHelper;

import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends RoboActivity
{
	@InjectView(R.id.gamebookList) ListView	gamebookList;
	@InjectView(R.id.buttonSettings) Button	buttonSettings;
	@InjectView(R.id.buttonAbout) Button	buttonAbout;
	
	private ArrayAdapter<String>			gamebookListAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		List<String> gamebooks = new ArrayList<String>();
		
		AssetManager assetManager = getAssets();
		try
		{
			for (String assetFile : assetManager.list("gamebooks"))
			{
				gamebooks.add(assetFile.substring(0, assetFile.lastIndexOf('.')));
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		gamebookListAdapter = new ArrayAdapter<String>(this, R.layout.listitem_text_single_line, R.id.text, gamebooks);
		gamebookList.setAdapter(gamebookListAdapter);
		gamebookList.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				StaticStorage.SelectedGamebook = (String) gamebookListAdapter.getItem(position);
				startActivity(new Intent(MainActivity.this, TitleActivity.class));
			}
		});
		
		buttonSettings.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				startActivity(new Intent(MainActivity.this, SettingsActivity.class));
			}
		});
		
		buttonAbout.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				startActivity(new Intent(MainActivity.this, AboutActivity.class));
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		boolean result;
		
		switch (item.getItemId())
		{
			case R.id.menu_settings:
			{
				startActivity(new Intent(MainActivity.this, SettingsActivity.class));
				result = true;
				break;
			}
			
			case R.id.menu_about:
			{
				startActivity(new Intent(MainActivity.this, AboutActivity.class));
				result = true;
				break;
			}
			
			default:
			{
				result = super.onOptionsItemSelected(item);
				break;
			}
		}
		
		return result;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		boolean result;
		
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if (ToastHelper.toastDoubleFinish(MainActivity.this))
			{
				MainActivity.this.finish();
			}
			result = true;
		}
		else
		{
			result = super.onKeyDown(keyCode, event);
		}
		
		return result;
	}
}
